package com.rongshu.view.home.fragment.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.rongshu.R;
import com.rongshu.base.BuildConfig;
import com.rongshu.utils.RandomCommentUtil;
import com.rongshu.view.home.listener.OnRecommendClickListener;
import com.widget.toast.ToastUtils;

import butterknife.BindView;

public class RecommendDialog extends Dialog {
    @BindView(R.id.edt_recommend)
    EditText edt_recommend;
    @BindView(R.id.btn_confirm)
    Button btn_confirm;

    public RecommendDialog(@NonNull Context context) {
        super(context);
    }

    public RecommendDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected RecommendDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        private Context context = null;
        private int mMainLayout = R.layout.fragement_recommend_card_dialog_view;
        private int propsCount = 0;
        private String content = null;
        private Button btn_confirm;
        private EditText edt_recommend;
        private OnRecommendClickListener mListener = null;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setPropsCount(int propsCount) {
            this.propsCount = propsCount;
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setOnRecommendClickListener(OnRecommendClickListener listener) {
            this.mListener = listener;
            return this;
        }

        public RecommendDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final RecommendDialog dialog = new RecommendDialog(context, R.style.AutoDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            edt_recommend = layout.findViewById(R.id.edt_recommend);
            edt_recommend.setText(content);
            edt_recommend.setFocusable(true);
            edt_recommend.setFocusableInTouchMode(true);
            edt_recommend.requestFocus();
            edt_recommend.setSelection(edt_recommend.getText().length());
            btn_confirm = layout.findViewById(R.id.btn_confirm);
            btn_confirm.setText("推荐卡(" + propsCount + ")");
            btn_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String contents = edt_recommend.getText().toString();
                    if (!TextUtils.isEmpty(contents)) {
                        if (mListener != null) {
                            dialog.dismiss();
                            mListener.onRecommendClick(1, 2, contents);
                        }
                    } else {
                        ToastUtils.show("请输入推荐语");
                    }
                }
            });
            dialog.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    String contents = edt_recommend.getText().toString();
                    if (mListener != null) {
                        mListener.onDismisssDialog(contents);
                    }
                }
            });
            dialog.setContentView(layout);
            dialog.getWindow().setWindowAnimations(R.style.DefaultAnimation);
            return dialog;
        }
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        this.getWindow().setAttributes(params);
    }

    @Override
    public void dismiss() {
        View view = getCurrentFocus();
        if (view instanceof EditText) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }

        super.dismiss();
    }
}
